package com.kkb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.common.utils.StringUtils;
import com.kkb.domain.AjaxResult;
import com.kkb.page.PageDomain;
import com.kkb.page.TableDataInfo;
import com.kkb.page.TableSupport;
import com.kkb.pojo.ChartData;
import com.kkb.pojo.TbRole;
import com.kkb.pojo.TbSubOrder;
import com.kkb.pojo.TbSysuser;
import com.kkb.service.RoleService;
import com.kkb.service.SysUserService;
import com.kkb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.kkb.domain.AjaxResult.error;
import static com.kkb.domain.AjaxResult.success;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;


    @RequestMapping("/index")
    public String index() {
        return "index";
    }
    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @RequestMapping("/logout")
    public String logout() {
        return "logout";
    }
    /**
     * 注册人数图表页面
     * @param
     * @return
     */
    @GetMapping("/registered")
    public String openChart() {
        return "user/registered";
    }

    @PostMapping("/chartData")
    @ResponseBody
    public List<ChartData> getChartData(){
        List<ChartData> register = userService.getRegister();
        return register;
    }

    /**
     * 人均消费统计
     * @return
     */
    @GetMapping("/consumption")
    public String consumption() {
        return "user/consumption";
    }

    @PostMapping("/consumptionData")
    @ResponseBody
    public List<ChartData> getConsumption(){
        List<ChartData> consumption = userService.getConsumption();
        return consumption;
    }


    @RequestMapping("/getUserList")
    @ResponseBody
    public TableDataInfo getUserList() {
        startPage();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        List<TbSysuser> orderLists = sysUserService.getUserList();
        rspData.setRows(orderLists);
        rspData.setTotal(new PageInfo(orderLists).getTotal());
        return rspData;
    }


    @RequestMapping("/userPage")
    public String showUserList(ModelMap mmap) {
        List<TbSysuser> userList = sysUserService.getUserList();
        mmap.addAttribute("userList", userList);
        return "system/user/user";
    }

    /**
     * 设置请求分页数据
     */
    private void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            PageHelper.startPage(pageNum, pageSize);
        }
    }
    /**
     * 根据id删除user
     */
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id")Integer sysUserId)
    {
        boolean result=sysUserService.deleteUserById(sysUserId);
        return result ? AjaxResult.success():AjaxResult.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    public AjaxResult batchRemove(@RequestBody List<Integer> ids)
    {
        AjaxResult result=sysUserService.batchDeleteUser(ids);
        return result;
    }

    /**
     * 跳转到编辑用户界面
     */
    @GetMapping("/getEditPage/{id}")
    public String edit(@PathVariable("id")Integer sysUserId, ModelMap mmap)
    {
        TbSysuser user = sysUserService.getUserById(sysUserId);
        List<TbRole> roleList = roleService.getRoleList();
        mmap.put("user", user);
        mmap.put("roles", roleList);
        return "system/user/edit";
    }

    /**
     * 跳转到新增用户界面
     */
    @GetMapping("/getAddPage")
    public String add()
    {
        return "system/user/add";
    }

    /**
     * 更新用户
     */
    @PostMapping("/editUser")
    @ResponseBody
    public AjaxResult editUser(@RequestBody TbSysuser sysuser)
    {

        boolean result=sysUserService.updateUser(sysuser);
        return result ? AjaxResult.success():AjaxResult.error();
    }

    /**
     * 添加用户
     */
    @PostMapping("/addUser")
    @ResponseBody
    public AjaxResult addUser(TbSysuser sysUser)
    {

        boolean result=sysUserService.addUser(sysUser);
        return result ? AjaxResult.success():AjaxResult.error();
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(TbSysuser user)
    {
        return sysUserService.checkLoginNameUnique(user.getUsername());
    }



    /**
     * 用户状态修改
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(TbSysuser user)
    {
        boolean result=sysUserService.changeStatus(user);
        return result ? AjaxResult.success():AjaxResult.error();
    }

    /**
     * 跳转至重置密码页面
     * @param sysUserId
     * @param mmap
     * @return
     */
    @GetMapping("/resetPwdPage/{sysuserId}")
    public String resetPwd(@PathVariable("sysuserId")Integer sysUserId, ModelMap mmap)
    {
        mmap.put("user", sysUserService.getUserById(sysUserId));
        return "system/user/resetPwd";
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(TbSysuser user){
        AjaxResult ajaxResult = sysUserService.resetPwd(user);
        return ajaxResult;
    }


}
